package edu.gatech.spacetraderteam3.view;

import java.awt.Graphics2D;

import edu.gatech.spacetraderteam3.model.GameState;


/**
 * The pause menu
 * @author Alexander Ikonomidis
 * @version 1.0
 */
public class ViewMenuOverlay extends View {
    /**
     * An interface for the pause menu
     * @author Alexander Ikonomidis
     * @version 1.0
     */
	public interface SGGMenuListener {
        /**
         * A resume function must be implemented for this interface
         */
		void resume();

		/**
		 * A save function must be implemented for this interface
		 * @return if the game has saved or not
		 */
        boolean save();

        /**
         * A quit function must be implemented for this interface
         */
        void quit();
    }

    /**
     * Listens to menu events
     */
    private SGGMenuListener menuListener;

    /**
     * the menu window and a dialog box
     */
    private final SGGWindow menu, messageDialog;
    /**
     * The message on a dialog box
     */
    private final SGGLabel messageLabel;

    /**
     * THe constructor for this overlay
     */
    public ViewMenuOverlay() {
        menu = new SGGWindow(0, 0, 200, 250, GameState.OVERLAY);
        menu.setAlignment(SGGWindow.Alignment.CENTER);

        SGGButton button = new SGGButton("RESUME");
        button.setSGGEventListener(new SGGEventListener() {
            public void doEvent(SGGEvent sgge) {
                if (menuListener != null) {
                    menuListener.resume();
                }
            }
        });
        menu.add(button, 57, 55);
        button = new SGGButton("SAVE");
        button.setSGGEventListener(new SGGEventListener() {
            public void doEvent(SGGEvent sgge) {
                if (menuListener != null) {
                    final boolean saved = menuListener.save();
                    showSaveMessage(saved);
                }

            }
        });
        menu.add(button, 57, 105);
        button = new SGGButton("QUIT");
        button.setSGGEventListener(new SGGEventListener() {
            public void doEvent(SGGEvent sgge) {
                if (menuListener != null) {
                    menuListener.quit();
                }
            }
        });
        menu.add(button, 57, 155);

        messageDialog = new SGGWindow(0, 0, 300, 120, GameState.OVERLAY);
        messageDialog.setAlignment(SGGWindow.Alignment.CENTER);
        button = new SGGButton("OKAY");
        button.setSGGEventListener(new SGGEventListener() {
            public void doEvent(SGGEvent sgge) {
                hideSaveMessage();
            }
        });
        messageDialog.add(button, 170, 40);
        messageDialog.setVisible(false);
        messageLabel = new SGGLabel("Placeholder text");
        messageDialog.add(messageLabel, 40, 50);
    }

    /**
     * Sets the listener for this menu overlay
     * @param menuListener
     */
    public void setSGGMenuListener(SGGMenuListener menuListener) {
        this.menuListener = menuListener;
    }

    /**
     * Hides the pop up save message
     */
    public void hideSaveMessage() {
        menu.setFocusable(true);
        messageDialog.setVisible(false);
    }

    /**
     * shows the pop up saved message
     * @param saved
     */
    private void showSaveMessage(boolean saved) {
        menu.setFocusable(false);
        messageDialog.setVisible(true);
        messageLabel.setText(saved ? "Save successful!" : "Save failed! :(");
    }

    /**
     * paints the current overlay onto the view
     * @param g - the graphics object
     */
    public void paint(Graphics2D g) {
        menu.paint(g);
        messageDialog.paint(g);
    }

    /**
     * @return this is the pause menu overlay
     */
    public String toString() {
        return "This is the pause menu overlay.";
    }
}
